import Vue from 'vue'
import { reqAddress ,reqFoodcategorys,reqLoginout,reqShops,reqUserInfo,reqShopGoods,reqShopInfo,reqShopRatings,repSearchShop} from '../api'
// 直接更新state中多个方法的对象
export default {
    // 根据经纬度获取位置详情 
    async RECEIVE_ADDRESS(state) {   //定义函数，将state的数据作为实参
        const geohash = state.latitude + ',' + state.longitude  //定义参数
        const result = await reqAddress(geohash)  //用await把变成同步函数  调用api/index.js中的reqAddress函数，将结果返回给result
        if (result.code == 0) {  //判断，当ajax接口问的code是0的时候
            const address = result.data  //将result的data数据传递得address
            state.address = address  //将数据传递个state
        //    result的数据是接口中的数据
        }
    },

    async RECEIVE_CATEGORYS(state) {
        // 异步请求Ajax接口
        const result = await reqFoodcategorys()
        if (result.code == 0) {
            const categorys = result.data
            state.categorys = categorys
        }
    },
    // 异步获取商家的信息
    async RECEIVE_SHOPS(state) {
        const {latitude,longitude} = state
        const result = await reqShops({latitude,longitude})
        if (result.code == 0) {
            const shops = result.data
            state.shops = shops
        }
       
    },
    //  // 异步获取用户的信息并保存到state中
    async RECEIVE_USERINFO(state){
        const result = await reqUserInfo()
        if (result.code == 0) {
            const userInfo = result.data
            state.userInfo=userInfo
            
        }
    }, 
    // 异步获取用户信息并且将信息置空
    async RECEIVE_USEROUT(state){
        const result = await reqLoginout()
        if (result.code == 0) {
            state.userInfo={}
        }
    },
    // 异步获取商家商品列表信息
    async RECEIVE_GOODS(state, cb){
        const result=await reqShopGoods()
        if(result.code==0){
            const goods=result.data
            state.goods=goods
            cb && cb()
        }
    },
    // 异步获取用户评价
    async RECEIVE_RATINGS(state, cb){
        const result=await reqShopRatings()
        if(result.code==0){
            const ratings=result.data
            state.ratings=ratings
            cb && cb()
        }
    },
    // 异步获取用户评价信息
    async RECEIVE_INFO(state){
        const result=await reqShopInfo()
        if(result.code==0){
            const info=result.data
            state.info=info
        }
    },
    // 购物车数字加一
    INCREMENTCOUNT(state,{food}){
        if(!food.count){
            // 只有第一次时才进行创建food.count
         Vue.set(food,'count',1)
        //  将当前实物放到数组中
         state.cartfoods.push(food)
            // console.log(1);
        }else{
            food.count++
            // console.log(1);
        }
    },
    // 购物车数字减一
    DECREMENTCOUNT(state,{food}){
        if(food.count){
            food.count--
            if(food.count===0){
                // 当数字等于0的时候就在catfoods数组中找到当前产品移出数组
                state.cartfoods.splice(state.cartfoods.indexOf(food),1)
            }
        }
    },
    CLEAR_CART(state){
        state.cartfoods.forEach(food => {
            food.count=0
        });
        state.cartfoods=[]
    },
    async SEARCH_SHOP(state,keyword){
    const geohash = state.latitude + ',' + state.longitude 
    const result = await repSearchShop({keyword,geohash })
    if(result.code===0){
        const searchShop =result.data
        state.searchShop=searchShop
    }

    }

}